package com.baixiaowen.server;

import com.baixiaowen.com.disruptor.MessageProducer;
import com.baixiaowen.com.disruptor.RingBufferWorkerPoolFactory;
import com.baixiaowen.com.entity.TranslatorData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        TranslatorData request = (TranslatorData) msg;
//        System.err.println("Server端：id = " + request.getId() + "; name = " + request.getName() + "; message = " + request.getMessage());
//        TranslatorData response = new TranslatorData();
//        response.setId("resp:" + request.getId());
//        response.setName("resp:" + request.getName());
//        response.setMessage("resp:" + request.getMessage());
//        // 写出response响应信息
//        ctx.writeAndFlush(response);

        TranslatorData request = (TranslatorData) msg;
        // 自己的应用服务应该有一个ID生成规则
        String producerId = "code:sessionId:001";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerId);
        messageProducer.onData(request, ctx);
    }
}
